package lib.servlet;

import lib.Dao.CourseDAO;
import lib.Dao.Dbutil;
import lib.Dao.UserDao;
import lib.Model.Course;
import lib.util.QRcode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ttop5 on 16-4-22.
 */
@WebServlet(urlPatterns = "/qrcode", name = "qrcode")
public class QrcodeServlet extends HttpServlet{
    Dbutil dbutil = new Dbutil();
    CourseDAO courseDAO=new CourseDAO();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       // String content = new String(req.getParameter("content").getBytes("ISO-8859-1"), "UTF-8");
        String openid= (String) req.getSession().getAttribute("openid");
        String cid= (String) req.getSession().getAttribute("cid");
        UserDao userDao=new UserDao();

        try {
            userDao.preuseradd(openid,cid);
            String url=QRcode.getQrUrl(openid);
            String qrurl="http://api.k780.com:88/?app=qr.get&data="+url+"&size=6";
            req.setAttribute("url",qrurl);
            req.getRequestDispatcher("/page/course/qrcode.jsp").forward(req, resp);
            //resp.sendRedirect("/page/dept/qrcode.jsp");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
